package sk.fiit.vppj.eventmanager.gui.administrator;

import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;
import java.io.IOException;
import java.util.logging.Level;
import java.util.logging.Logger;

import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JFileChooser;
import javax.swing.JLabel;
import javax.swing.JPanel;

import sk.fiit.vppj.eventmanager.graph.ChartType;
import sk.fiit.vppj.eventmanager.graph.Graph;
import sk.fiit.vppj.eventmanager.gui.MainFrame;

/**
 * Panel which creates Statistics tab
 * 
 * @author Marek
 * 
 */
final class AdminStatsPanel extends JPanel {

	private JLabel graphIcon = new JLabel();
	private final static Logger log = Logger.getLogger( AdminStatsPanel.class.getName() );

	public AdminStatsPanel() {
		setLayout( new FlowLayout( FlowLayout.CENTER ) );
		JPanel panel = new JPanel();
		final JLabel statisticType = new JLabel( "Statistic type : " );
		final JComboBox statisticTypeBox = new JComboBox( new String[] { "BIRTHDAY", "REGISTRATION", "LAST_VISIT",
				"PARTICIPANTS_PER_STATES", "FUNTRIP_VISITORS", "ESITOUR_VISITORS", "PROJECTS_SCORE", "ACCOUNT_TYPE" } );
		panel.add( statisticType );
		panel.add( statisticTypeBox );

		final JLabel statisticGroupBy = new JLabel( "Group by : " );
		final JComboBox statisticGroupByBox = new JComboBox( new String[] { "YEAR", "MONTH", "DAY" } );
		panel.add( statisticGroupBy );
		panel.add( statisticGroupByBox );

		statisticTypeBox.addActionListener( new ActionListener() {

			@Override
			public void actionPerformed(ActionEvent e) {
				if( statisticTypeBox.getSelectedItem().toString().equals( "BIRTHDAY" )
						|| statisticTypeBox.getSelectedItem().toString().equals( "REGISTRATION" )
						|| statisticTypeBox.getSelectedItem().toString().equals( "LAST_VISIT" ) ) {
					statisticGroupByBox.setVisible( true );
					statisticGroupBy.setVisible( true );

				}
				else {
					statisticGroupBy.setVisible( false );
					statisticGroupByBox.setVisible( false );
				}
			}
		} );

		JLabel graphType = new JLabel( "Graph type : " );
		final JComboBox graphTypeBox = new JComboBox( new Object[] { ChartType.VERTICAL_BAR, ChartType.LINE_CHART_BASIC } );
		panel.add( graphType );
		panel.add( graphTypeBox );

		JButton showDrawButton = new JButton( "Draw graph" );
		showDrawButton.addActionListener( new ActionListener() {

			@Override
			public void actionPerformed(ActionEvent e) {
				// preview version, just to demostrate google graph api... bad design!!!!
				Graph graph = new Graph( statisticTypeBox.getSelectedItem().toString(), statisticGroupByBox.getSelectedItem().toString() );

				ImageIcon icon = graph.getIcon( ChartType.valueOf( graphTypeBox.getSelectedItem().toString() ) );
				graphIcon.setIcon( icon );

				if( icon == null ) {
					graphIcon.setText( "Internet connection not available" );
				}

				updateUI();
			}
		} );
		panel.add( showDrawButton );

		JButton showExportButton = new JButton( "Export graph" );
		showExportButton.addActionListener( new ActionListener() {

			@Override
			public void actionPerformed(ActionEvent e) {
				JFileChooser fch = new JFileChooser();

				int returnValue = fch.showOpenDialog( MainFrame.getInstance() );
				if( returnValue == JFileChooser.APPROVE_OPTION ) {
					try {
						File file = new File( fch.getSelectedFile().getCanonicalPath() );
						Graph graph = new Graph( statisticTypeBox.getSelectedItem().toString(), statisticGroupByBox.getSelectedItem()
								.toString() );
						graph.saveToXml( file, statisticTypeBox.getSelectedItem().toString() );

					}
					catch ( IOException e1 ) {
						log.log( Level.WARNING, "Xml creation for graphs has failed", e );
						e1.printStackTrace();
					}

				}
			}
		} );
		panel.add( showExportButton );

		add( panel );
		add( graphIcon );

	}
}
